;(function() {
  var menus = document.querySelectorAll('.type-menu__item')
  var itemList = document.querySelectorAll('.help-list__box')
  addEvent(menus, 'click', function(e, index) {
    // 菜单
    removeClass(menus, 'cur')
    addClass(menus[index], 'cur')
    // 内容
    addClass(itemList, 'd-none')
    removeClass(itemList[index], 'd-none')
    getContentHeight()
  })
  init()
})()

function init() {
  getContentHeight()
  toggleItem()
}

// 获取item隐藏内容高度
function getContentHeight() {
  var item = document.querySelectorAll('.help-item .item-content')
  for (var i = 0; i < item.length; i++) {
    var height = item[i].clientHeight
    item[i].setAttribute('data-height', height)
  }
}

// 开关内容
function toggleItem() {
  var item = document.querySelectorAll('.help-item .item-title')
  addEvent(item, 'click', function(e, index) {
    var appendHeight = item[index].nextElementSibling.getAttribute(
      'data-height'
    )
    var parent = item[index].parentElement
    var parentHeight = parent.clientHeight
    var toggleHeight = 0
    if (isExpand(item[index])) {
      toggleHeight = parseInt(parentHeight) + parseInt(appendHeight)
    } else {
      toggleHeight = parseInt(parentHeight) - parseInt(appendHeight)
    }
    parent.style.height = toggleHeight + 'px'
  })
}

// 判断是否打开
function isExpand(target) {
  var is_expand = target.getAttribute('data-toggle')
  var icon = target.children[0]
  if (is_expand === '1') {
    target.setAttribute('data-toggle', 0)
    icon.style.cssText = 'transform:rotate(0deg) translateY(-50%)'
    return false
  } else {
    target.setAttribute('data-toggle', 1)
    icon.style.cssText = 'transform:rotate(90deg) translateX(-50%)'
    return true
  }
}
